package cn.szq.concurrent.thread.countdownlatch;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.IntStream;

/**
 * @ClassName: RunDemo2
 * @Description:
 * @Author: szq
 * @Date: 2021-10-25 08:31:37
 */
public class RunDemo2 {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("运动员有5秒准备时间。");
        CountDownLatch cdl = new CountDownLatch(1);
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        IntStream.rangeClosed(1, 5).forEach(t->{
            final int num = t;
            Runnable runnable = ()->{
                System.out.println(num + "号运动员准备完毕。");
                try{
                    cdl.await();
                    System.out.println(num + "号运动员开始跑步了。");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            };
            executorService.submit(runnable);
        });
        Thread.sleep(5000);
        System.out.println("5秒准备时间已过，发令枪响，比赛开始！");
        cdl.countDown();
    }
}
